﻿using System;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Data;

namespace LpbPrj.Client.Tools.Converter
{
    public class Object2EnabledConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool output = false;

            if (value == null)
            {
                output = parameter == null ? false : true;
            }
            else
            {
                output = parameter == null ? true : false;
            }


            return output;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }


    public class EqualEnabledConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null || parameter != null)
            {
                int data1 = System.Convert.ToInt32(value);
                int data2 = System.Convert.ToInt32(parameter);
                if (data1 == data2)
                {
                    return true;
                }
            }
            return false;
        }

        public object ConvertBack(object value, Type targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class EqualDisabledConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null || parameter != null)
            {
                int data1 = System.Convert.ToInt32(value);
                int data2 = System.Convert.ToInt32(parameter);
                if (data1 == data2)
                {
                    return false;
                }
            }
            return true;
        }

        public object ConvertBack(object value, Type targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }


    /// <summary>
    /// 多绑定 有一个为true 则显示，否则隐藏，参数为0时，逻辑翻转
    /// </summary>
    public class MultiOrEqualEnabledConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            bool output = false;

            if (values != null && values.Count() > 0)
            {
                {
                    bool param = true;
                    if (parameter != null)
                    {
                        param = System.Convert.ToBoolean(parameter);
                    }
                    var count = values.Where(p => p != DependencyProperty.UnsetValue && p != null && System.Convert.ToBoolean(p)).Count();
                    if (count == 0)
                    {
                        output = !param;
                    }
                    else
                    {
                        output = param;
                    }
                }
            }
            return output;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 多绑定 都为true 则显示，否则隐藏，参数为0时，逻辑翻转
    /// </summary>
    public class MultiAndEqualEnabledConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            bool output = false;

            if (values != null && values.Count() > 0)
            {
                {
                    bool param = true;
                    if (parameter != null)
                    {
                        param = System.Convert.ToBoolean(parameter);
                    }
                    var count = values.Where(p => p != DependencyProperty.UnsetValue && p != null && System.Convert.ToBoolean(p)).Count();
                    if (count == values.Count())
                    {
                        output = param;
                    }
                    else
                    {
                        output = !param;
                    }
                }
            }
            return output;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }


}
